home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / Icon 8.1 / mep1 / Samples / Programs / parens.icn < prev    next >
Encoding:
Text File  |  1989-05-09  |  2.7 KB  |  104 lines  |  [TEXT/PICN]

  1. ############################################################################
  2. #
  3. #  parens.icn
  4. #  
  5. #     This program produces parenthesis-balanced strings in which
  6. #  the parentheses are randomly distributed.
  7. #  
  8. #  Options: The following parameter string options are available:
  9. #  
  10. #       -b n Bound the length of the strings to n left and right
  11. #            parentheses each. The default is 10.
  12. #  
  13. #       -n n Produce n strings. The default is 10.
  14. #  
  15. #       -l s Use the string s for the left parenthesis. The default
  16. #            is ( .
  17. #  
  18. #       -r s Use the string s for the right parenthesis. The default
  19. #            is ) .
  20. #  
  21. #       -v   Randomly vary the length of the strings between 0 and
  22. #            the bound.  In the absence of this option, all strings
  23. #            are the exactly as long as the specified bound.
  24. #  
  25. #     For example, the output for the parameter string
  26. #  
  27. #          -v -b 4 -l "begin " -r "end "
  28. #  
  29. #  is
  30. #  
  31. #          begin end
  32. #          begin end begin end
  33. #          begin begin end end begin end
  34. #          begin end begin begin end end
  35. #          begin end
  36. #          begin begin end end
  37. #          begin begin begin end end end
  38. #          begin end begin begin end end
  39. #          begin end begin end
  40. #          begin begin end begin end begin end end
  41. #  
  42. #  
  43. #  Comments: This program was motivated by the need for test data
  44. #  for error repair schemes for block-structured programming lan-
  45. #  gauges. A useful extension to this program would be some
  46. #  way of generating other text among the parentheses.  In addition
  47. #  to the intended use of the program, it can produce a variety of
  48. #  interesting patterns, depending on the strings specified by -l
  49. #  and -r.
  50. #  
  51. ############################################################################
  52. #
  53. #  Links: getopt
  54. #
  55. ############################################################################
  56.  
  57. link getopt
  58.  
  59. global r, k, lp, rp
  60.  
  61. procedure main(args)
  62.     local string, i, s, bound, limit, varying, opts
  63.     
  64.     bound := limit := 10            # default bound and limit
  65.     lp := "("                        # default left paren
  66.     rp := ")"                        # default right paren
  67.  
  68.     opts := getopt(args,"l:r:vb+n+")[1]
  69.     bound := \opts["b"] | 10
  70.     limit := \opts["n"] | 10
  71.     lp := \opts["l"] | "("
  72.     rp := \opts["r"] | ")"
  73.     varying := opts["v"]
  74.     
  75.     every 1 to limit do {
  76.         if \varying then k := 2 * ?bound else k := 2 * bound
  77.         string := ""
  78.         r := 0
  79.         while k ~= r do {
  80.             if r = 0 then string ||:= Open()
  81.             else if ?0 < probClose()
  82.                 then string ||:= Close() else string ||:= Open()
  83.             }
  84.         while k > 0 do string ||:= Close()
  85.         write(string)
  86.         }
  87. end
  88.  
  89. procedure Open()
  90.     r +:= 1
  91.     k -:= 1
  92.     return lp
  93. end
  94.  
  95. procedure Close()
  96.     r -:= 1
  97.     k -:= 1
  98.     return rp
  99. end
  100.  
  101. procedure probClose()
  102.     return ((r * (r + k + 2)) / (2.0 * k * (r + 1)))
  103. end
  104.